using System;
using System.Collections.Generic;
using System.Text;

//design time
using System.ComponentModel.Design;
using System.ComponentModel;
using System.Drawing.Design;
using System.Collections;

namespace Adoor.Object.Domain
{
    [Serializable, DefaultProperty("Items")
    //,Editor(typeof(EditorTest3), typeof(UITypeEditor))
    ] //Editor("Adoor.VSDesigner.CollectionEditor.LinkedDomainCollectionEditor, Adoor.VSDesigner", typeof(UITypeEditor))]
    public class ObjectDomainCollection : CollectionBase, IList<ObjectDomain>, ICollection<ObjectDomain>, IEnumerable<ObjectDomain>, ICustomTypeDescriptor
    {
        private Adoor.Collections.Generic.List<ObjectDomain> m_innerList = new Adoor.Collections.Generic.List<ObjectDomain>();

        public ObjectDomainCollection()
        {
            m_innerList = new Adoor.Collections.Generic.List<ObjectDomain>();
            m_innerList.CollectionChanged += new Adoor.Collections.Generic.CollectionChangeEventHandler<ObjectDomain>(m_innerList_CollectionChanged); 
        }

        void m_innerList_CollectionChanged(object sender, Adoor.Collections.Generic.CollectionChangeEventArgs<ObjectDomain> e)
        {
            if (this.CollectionChanged != null)
                this.CollectionChanged(sender,e);
        }


        public event Adoor.Collections.Generic.CollectionChangeEventHandler<ObjectDomain> CollectionChanged;

        [Browsable(false), 
        System.ComponentModel.EditorBrowsable(EditorBrowsableState.Never)]
        public Adoor.Collections.Generic.List<ObjectDomain> Items
        {
            get { return m_innerList; }
        }

        public IObjectResolver objectResolver;

        [Browsable(false)] 
        public IObjectResolver ObjectResolver
        {
            get
            {
                return objectResolver;
            }
            set
            {
                if (value != this.objectResolver)
                {
                    objectResolver = value;    
                }
            }
        }


        #region IList<ObjectDomain> Members

        public int IndexOf(ObjectDomain item)
        {
            return m_innerList.IndexOf(item);
        }

        public void Insert(int index, ObjectDomain item)
        {
            m_innerList.Insert(index, item);
        }

        public void RemoveAt(int index)
        {
            m_innerList.RemoveAt(index);
        }

        public ObjectDomain this[int index]
        {
            get
            {
                return m_innerList[index];
            }
            set
            {
                m_innerList[index] = value;
            }
        }

        #endregion

        #region ICollection<ObjectDomain> Members

        public void Add(ObjectDomain item)
        {
            m_innerList.Add(item);
            //if (this.objectResolver != null)
            //    item.objectResolver = this.objectResolver;
        }

        

        public void Clear()
        {
            m_innerList.Clear();
        }

        public bool Contains(ObjectDomain item)
        {
            return m_innerList.Contains(item);
        }

        public void CopyTo(ObjectDomain[] array, int arrayIndex)
        {
            m_innerList.CopyTo(array, arrayIndex);
        }

        public int Count
        {
            get { return m_innerList.Count; }
        }

        public bool IsReadOnly
        {
            get { return m_innerList.IsReadOnly; }
        }

        public bool Remove(ObjectDomain item)
        {
            return m_innerList.Remove(item);
        }

        #endregion

        #region IEnumerable<ObjectDomain> Members

        public IEnumerator<ObjectDomain> GetEnumerator()
        {
            return m_innerList.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return (m_innerList as System.Collections.IEnumerable).GetEnumerator(); ;
        }

        #endregion

        #region ICustomTypeDescriptor Members

        public AttributeCollection GetAttributes()
        {
            return TypeDescriptor.GetAttributes(this, true);
        }

        public string GetClassName()
        {
            return TypeDescriptor.GetClassName(this, true);
        }

        public string GetComponentName()
        {
            return TypeDescriptor.GetComponentName(this, true);
        }

        public TypeConverter GetConverter()
        {
            return TypeDescriptor.GetConverter(this, true);
        }

        public EventDescriptor GetDefaultEvent()
        {
            return TypeDescriptor.GetDefaultEvent(this, true);
        }

        public PropertyDescriptor GetDefaultProperty()
        {
            return TypeDescriptor.GetDefaultProperty(this, true);
        }

        public object GetEditor(Type editorBaseType)
        {
            return TypeDescriptor.GetEditor(this, editorBaseType, true);
        }

        public EventDescriptorCollection GetEvents(Attribute[] attributes)
        {
            return TypeDescriptor.GetEvents(this, attributes, true);
        }

        public EventDescriptorCollection GetEvents()
        {
            return TypeDescriptor.GetEvents(this, true);
        }

        public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
        {
            return TypeDescriptor.GetProperties(this, attributes, true);
        }

        public PropertyDescriptorCollection GetProperties()
        {
            return TypeDescriptor.GetProperties(this, true);
        }

        public object GetPropertyOwner(PropertyDescriptor pd)
        {
            return this;
        }

        #endregion
    }
}
